/**
 * 
 */
package edu.umd.clip.lm.storage.remote;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class FailureResponseMessage extends Message {
	private int code;
	private String message;
	
	public static final int NOT_FOUND = 1;
	public static final int ERROR = 2;
	/**
	 * @param type
	 */
	public FailureResponseMessage(int modelId, int code, String message, int replyTo) {
		super(MessageHeader.FAILURE_RESPONSE, modelId);
		setReplyTo(replyTo);
		this.code = code;
		this.message = message;
	}

	public FailureResponseMessage(MessageHeader header, ByteBuffer buffer) {
		super(header);
		readBody(buffer);
	}
	
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.storage.remote.Message#readBody(java.nio.ByteBuffer)
	 */
	@Override
	protected void readBody(ByteBuffer buffer) {
		code = buffer.getInt();
		byte bytes[] = buffer.array();
		message = new String(bytes, buffer.position(), buffer.remaining(), Charset.forName("UTF-8"));
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.storage.remote.Message#writeBody()
	 */
	@Override
	protected ByteBuffer writeBody() {
		byte msgBytes[] = message.getBytes(Charset.forName("UTF-8"));
		ByteBuffer buffer = ByteBuffer.allocate(4 + msgBytes.length);
		buffer.putInt(code);
		buffer.put(msgBytes);
		return buffer;
	}

	public int getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}
	
}
